package club.sinoexcel.service.impl;

import club.sinoexcel.cxf.domain.User;
import club.sinoexcel.service.IUserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements IUserService {
    @Override
    public void save(User user) {
        System.out.println("用户已保存："+user);
    }

    @Override
    public void deleteById(Integer id) {
        System.out.println("用户已删除："+id);
    }

    @Override
    public void update(User user) {
        System.out.println("用户已更新："+ user);
    }

    @Override
    public User findById(Integer id) {
        User user = new User();
        user.setId(1);
        user.setName("中网优训");
        user.setAge(1);
        return user;
    }

    @Override
    public String findAll() {
        List<User> list = new ArrayList<>();
        User user = new User();
        user.setId(1);
        user.setName("中网优训");
        user.setAge(1);
        list.add(user);

        user = new User();
        user.setId(2);
        user.setName("中网优训2");
        user.setAge(2);
        list.add(user);

        user = new User();
        user.setId(3);
        user.setName("中网优训3");
        user.setAge(3);
        list.add(user);

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String s = objectMapper.writeValueAsString(list);
            return s;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "";
        }
    }

    @Override
    public List<User> findList() {
        List<User> list = new ArrayList<>();
        User user = new User();
        user.setId(1);
        user.setName("中网优训");
        user.setAge(1);
        list.add(user);

        user = new User();
        user.setId(2);
        user.setName("中网优训2");
        user.setAge(2);
        list.add(user);

        user = new User();
        user.setId(3);
        user.setName("中网优训3");
        user.setAge(3);
        list.add(user);

       return list;
    }
}
